package org.itboys.mobile.controller.small;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.product.SundayProductCategoryDto;
import org.itboys.mobile.entity.mysql.SundayProductCategory;
import org.itboys.mobile.service.mysql.weixin.SundayWeiXinProductCategoryService;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：zzq
 * 时间：2018/4/30
 * 描述：
 */
@Controller
@RequestMapping("/sunday/small/product/category")
@Api(value = "product",description = "楼盘区域")
public class SundaySmallProductCategoryController extends BaseController{
    @Autowired
    private SundayWeiXinProductCategoryService categoryService;
    @RequestMapping(value="/select", method= {RequestMethod.POST})
    @ApiOperation(value="获取楼盘区域列表")
    public void select(
            HttpServletRequest request,
            HttpServletResponse response
    ){
        try{
            List<SundayProductCategoryDto> result=categoryService.selectCategory();
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/product/category/select----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    @RequestMapping(value="/findOne", method= {RequestMethod.POST})
    @ApiOperation(value="获取单个楼盘区域")
    public void findOne(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "categoryId", value = "id") @RequestParam(value = "categoryId", required = true) Long  categoryId
    ){
        try{
            SundayProductCategory result=categoryService.findOne(categoryId);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/product/category/findOne----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }


}
